package com.cjj.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cjj.common.convention.result.Result;
import com.cjj.common.convention.result.Results;
import com.cjj.common.enums.OrderType;
import com.cjj.dao.entity.RunnerOrder;
import com.cjj.dto.req.AcceptRunnerOrderReqDTO;
import com.cjj.dto.req.CreateRunnerOrderReqDTO;
import com.cjj.dto.req.CreateServiceOrderDTO;
import com.cjj.dto.req.UpdateByRunnerOrderIdReqDTO;
import com.cjj.service.RunnerOrderService;
import com.cjj.service.ServiceOrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 跑腿订单表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2025-10-25
 */
@RestController
@RequestMapping("/api/runnerOrder")
@RequiredArgsConstructor//构造器注入
public class RunnerOrderController {

    private final RunnerOrderService runnerOrderService;
    private final ServiceOrderService serviceOrderService;

    /**
    创建跑腿订单
     */
    @PostMapping("/create")
    public Result<Void> createRunnerOrder(@RequestBody CreateRunnerOrderReqDTO createRunnerOrderReqDTO) {
        //先创建订单
        CreateServiceOrderDTO serviceOrderDTO = BeanUtil.copyProperties(createRunnerOrderReqDTO, CreateServiceOrderDTO.class);
        serviceOrderDTO.setType(OrderType.RUNNER.getValue());
        serviceOrderService.createOrder(serviceOrderDTO);
        createRunnerOrderReqDTO.setOrderId(serviceOrderDTO.getOrderId());

        //创建跑腿订单
        runnerOrderService.createOrder(createRunnerOrderReqDTO);
        return Results.success();
    }

    /**
     * 根据跑腿订单号修改订单金额
     */
    @PutMapping("/update")
    public Result<Void> updateByOrderId(@RequestBody UpdateByRunnerOrderIdReqDTO updateByRunnerOrderIdReqDTO) {
        runnerOrderService.updateByOrderId(updateByRunnerOrderIdReqDTO);
        return Results.success();
    }

    /**
     * 骑手浏览跑腿需求列表
     */
    @GetMapping("/available")
    public Result<IPage<RunnerOrder>> getAvailableOrders
    ( @RequestParam(defaultValue = "1") Integer page,
      @RequestParam(defaultValue = "10") Integer size){
        IPage<RunnerOrder> runnerOrderIPage = runnerOrderService.getAvailableOrders(page, size);
        return Results.success(runnerOrderIPage);
    }

    /**
     * 骑手接单
     */
    @PutMapping("/acceptRunnerOrder")
    public Result<Void> acceptRunnerOrder(@RequestBody AcceptRunnerOrderReqDTO acceptRunnerOrderReqDTO){
        runnerOrderService.acceptRunnerOrder(acceptRunnerOrderReqDTO);
        return Results.success();
    }

    /**
     * 骑手完成订单
     */
    @PutMapping("/{orderId}/completed")
    public Result<Void> completeRunnerOrder(@PathVariable Long orderId){
        runnerOrderService.completeRunnerOrder(orderId);
        return Results.success();
    }
}